/**
 * Created by DevilsEyes on 2016/3/16.
 */
//这里处理微信的相关操作
'use strict';

define(['wxsdk', 'ex'], function (wx, ex) {

    var wxready = false;
    var cb;

    if (agent.isWechat) {
        var getWxSign = function () {
            var wxUrl = encodeURIComponent(location.href.split('#')[0]);
            ex.jsonp({
                url: api + 'wx/jsSDKTicket',
                data: {
                    url: wxUrl
                },
                success: function (obj) {
                    var appId = obj.data.app_id,
                        timestamp = obj.data.timestamp,
                        nonceStr = obj.data.noncestr,
                        signature = obj.data.signature;
                    wx.config({
//                        debug: true,
                        appId: appId,
                        timestamp: timestamp,
                        nonceStr: nonceStr,
                        signature: signature,
                        jsApiList: [
                            'onMenuShareTimeline',
                            'onMenuShareAppMessage',
                            'onMenuShareQQ',
                            'onMenuShareWeibo'
                        ]
                    });
                    wx.ready(function () {
                        mod.check.resolve();
                    })
                }
            });
        };

        getWxSign();
    }

    wx.ready(function () {
        wxready = true;
        if (typeof cb == 'function')cb();
    });

    var mod = {
        ready: function (callback) {
            if (wxready) {
                callback();
            } else {
                cb = callback;
            }
        },
        shareAllow: function () {
            wx.showOptionMenu();
            wx.hideAllNonBaseMenuItem();
            wx.showMenuItems({
                menuList: ['onMenuShareTimeline'] // 要显示的菜单项，所有menu项见附录3
            });
        },
        closeAll: function () {
            wx.hideOptionMenu();
            wx.hideAllNonBaseMenuItem();
        },
        setShareParams: function (options) {
            //todo 分享参数的设置;
        },
        check: new Promise()
    };

    return mod;
});